package com.adnaan.backend.controller;

import com.adnaan.backend.entity.SysUser;
import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * author: adnaan
 * create: 2024-04-22 20:35
 * Description:
 */
public class BaseController {

    @Autowired
    protected HttpServletRequest request;

    @Resource
    protected StringRedisTemplate stringRedisTemplate;

    // 从request中获取用户id
    public Long getUserId() {
        return (Long) request.getAttribute("userId");
    }


    // 获取当前登录用户信息
    public SysUser getCurUser() {
        String token = request.getHeader("Authorization");
        String userJson = stringRedisTemplate.opsForValue().get(token);
        return JSON.parseObject(userJson,SysUser.class);
    }


}

